package org.easy.tool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.easy.tool.common.*;
import org.easy.tool.entity.Knowledge;
import org.easy.tool.entity.Problem;
import org.easy.tool.entity.ProblemKnowledge;
import org.easy.tool.mapper.KnowledgeMapper;
import org.easy.tool.mapper.ProblemMapper;
import org.easy.tool.mapper.ProblemKnowledgeMapper;
import org.easy.tool.service.IProblemKnowledge;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Valley
 * @since 2024-09-04 22:51:14
 */
@Service
public class ProblemKnowledgeService extends ServiceImpl<ProblemKnowledgeMapper, ProblemKnowledge> implements IProblemKnowledge {

    @Autowired
    KnowledgeMapper knowledgeMapper;

    @Autowired
    ProblemMapper problemMapper;

    @Autowired
    ProblemKnowledgeMapper problemKnowledgeMapper;

    @Override
    public List<Knowledge> getProblemKnowledgeList(LinkedHashMap data) {
        String problemId = (String) data.get("problemId");
        return getProblemKnowledgeList(problemId);
    }

    @Override
    public List<Knowledge> getProblemKnowledgeList(String problemId) {
        LambdaQueryWrapper<ProblemKnowledge> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProblemKnowledge::getProblemId, problemId);
        return list(wrapper).stream().map(
                item -> knowledgeMapper.selectById(item.getKnowledgeId())
        ).collect(Collectors.toList());
    }

    @Override
    public boolean editProblemKnowledge(LinkedHashMap data) {
        String problemId = (String) data.get("problemId");
        List<String> knowledgeList = (List<String>) data.get("KnowledgeList");
        LambdaQueryWrapper<ProblemKnowledge> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProblemKnowledge::getProblemId, problemId);
        remove(wrapper);
        for (String KnowledgeId : knowledgeList) {
            addProblemKnowledge(problemId, KnowledgeId);
        }
        return true;
    }

    @Override
    public boolean addProblemKnowledge(String problemId, String KnowledgeId) {
        ProblemKnowledge problemKnowledge = new ProblemKnowledge();
        problemKnowledge.setProblemId(problemId);
        problemKnowledge.setKnowledgeId(KnowledgeId);
        problemKnowledge.setUserId(WebUtil.getUserId());
        problemKnowledge.setCreateTime(TimeUtil.nowMS());
        problemKnowledge.setUpdateTime(TimeUtil.nowMS());
        return save(problemKnowledge);
    }

    @Override
    public boolean removeProblemKnowledge(String problemId, String KnowledgeId) {
        LambdaQueryWrapper<ProblemKnowledge> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProblemKnowledge::getProblemId, problemId);
        wrapper.eq(ProblemKnowledge::getKnowledgeId, KnowledgeId);
        return remove(wrapper);
    }

    public List<Problem> getProblemList(LinkedHashMap data) {
        Object andList = data.get("andList");
        List<Problem> filterProblem = new ArrayList<>();
        List<String> filterKnowledgeList = (List<String>) andList;
        List<LinkedHashMap> mapList = mergeProblemKnowledge();
        for (LinkedHashMap map : mapList) {
            List<String> problemKnowledgeList = (List<String>) map.get("KnowledgeList");
            if (problemKnowledgeList.containsAll(filterKnowledgeList)) {
                String problemId = (String) map.get("problemId");
                filterProblem.add(problemMapper.selectById(problemId));
            }
        }
        return filterProblem;
    }

    /**
     * 获取某个科目下的所有题目
     */
    private List<Problem> getProblemBySubject(String subjectId) {
        //章节
        List<Knowledge> chapterList = knowledgeMapper.getKnowledgeListByPid(subjectId);
        //知识点
        List<Knowledge> knowledgeList = new ArrayList<>();
        for (Knowledge chapter : chapterList) {
            List<Knowledge> knowledgeListByPid = knowledgeMapper.getKnowledgeListByPid(chapter.getId());
            knowledgeList.addAll(knowledgeListByPid);
        }
        //题目
        List<Problem> dataList = new ArrayList<>();
        for (Knowledge knowledge : knowledgeList) {
            List<ProblemKnowledge> problemList = problemKnowledgeMapper.getProblemKnowledgeByKnowledgeId(knowledge.getId());
            for (ProblemKnowledge problemKnowledge : problemList) {
                Problem problem = problemMapper.selectById(problemKnowledge.getProblemId());
                dataList.add(problem);
            }
        }
        return dataList;
    }

    @Override
    public Object exam(LinkedHashMap data) {
        String subjectId = (String) data.get("subjectId");
        List<Problem> problemBySubject = getProblemBySubject(subjectId);
        List<LinkedHashMap> dataList = new ArrayList<>();
        for (Problem problem : problemBySubject) {
            LinkedHashMap formData = new LinkedHashMap();
            formData.put("id", problem.getId());
            formData.put("title", problem.getTitle());
            formData.put("problemImg", TihaiUtil.getProblemImg(problem.getContent()));
            formData.put("explainImg", TihaiUtil.getExplainImg(problem.getContent()));
            dataList.add(formData);
        }
        ListUtil.shuffle(dataList);
        return dataList;
    }

    @Override
    public Object getSubjectWithProblem(LinkedHashMap data) {
        List<Knowledge> dataList = new ArrayList<>();
        List<Knowledge> subjectList = knowledgeMapper.getAllSubject();
        for (Knowledge subject : subjectList) {
            List<Problem> problemBySubject = getProblemBySubject(subject.getId());
            if (problemBySubject.size() > 0) {
                dataList.add(subject);
            }
        }
        return dataList;
    }


    @Override
    public Object test1() {
        LambdaQueryWrapper<Problem> problemWrapper = new LambdaQueryWrapper<>();
        List<Problem> problemList = problemMapper.selectList(problemWrapper);
        for (Problem problem : problemList) {
            if (StringUtil.isBlank(problem.getCreateTime())) {
                problem.setCreateTime(TimeUtil.nowMS());
            }
            if (StringUtil.isBlank(problem.getUpdateTime())) {
                problem.setUpdateTime(TimeUtil.nowMS());
            }
            problemMapper.updateById(problem);
        }
        return true;
    }

    @Override
    public Object batchAddByProblemId(LinkedHashMap data) {
        String problemId = (String) data.get("problemId");
        List<String> knowledgeIdList = (List<String>) data.get("knowledgeIdList");
        problemKnowledgeMapper.deleteByProblemId(problemId);
        for (String knowledgeId : knowledgeIdList) {
            ProblemKnowledge problemKnowledge = new ProblemKnowledge();
            problemKnowledge.setProblemId(problemId);
            problemKnowledge.setKnowledgeId(knowledgeId);
            problemKnowledge.setCreateTime(TimeUtil.nowMS());
            problemKnowledge.setUpdateTime(TimeUtil.nowMS());
            save(problemKnowledge);
        }
        return true;
    }

    @Override
    public List<LinkedHashMap> mergeProblemKnowledge() {
        List<Problem> problemList = problemMapper.selectList(new LambdaQueryWrapper<>());
        List<LinkedHashMap> mergeList = new ArrayList<>();
        for (Problem problem : problemList) {
            LinkedHashMap merge = new LinkedHashMap();
            List<Knowledge> problemKnowledgeList = getProblemKnowledgeList(problem.getId());
            if (problemKnowledgeList.size() > 0) {
                merge.put("problemId", problem.getId());
                List<String> collect = problemKnowledgeList.stream().map(Knowledge::getName).collect(Collectors.toList());
                merge.put("KnowledgeList", collect);
                mergeList.add(merge);
            }
        }
        return mergeList;
    }


}
